package scripts;

import api.CommonApi;
import api.LoginApi;
import api.UserApi;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import utils.ResponseData;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;

public class TestAddUser {
    private static final Logger log = LoggerFactory.getLogger(TestAddUser.class);
    private final UserApi userApi = new UserApi();
    private final CommonApi commonApi = new CommonApi();
    private final LoginApi loginApi = new LoginApi();

    @BeforeClass
    public void beforeClass() throws Exception {
        // 获取图片验证码
        commonApi.getVerifyCode(String.valueOf(new Random().nextDouble()));
        // 登录成功
        loginApi.login("admin", "12345678", "8888");
    }

    @DataProvider
    public Object[][] buildData() throws IOException {
        // 读取json文件
        String jsonStr = FileUtils.readFileToString(new File("./data/addUser.json"), "UTF-8");

        // 解析json数据，组装数据
        JSONArray jsonArray = JSONObject.parseArray(jsonStr);
        Object[][] testData = new Object[jsonArray.size()][];
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            testData[i] = new Object[]{jsonObject.get("name"), jsonObject.get("phone"), jsonObject.get("pwd"),
                    jsonObject.get("rePwd"), jsonObject.get("role"), jsonObject.get("statusCode"),
                    jsonObject.get("status"), jsonObject.get("description")};
        }
        log.info("testData={}", Arrays.deepToString(testData));
        return testData;
    }

    @Test(dataProvider = "buildData")
    public void testAddUser(String name, String phone, String pwd, String rePwd, int role, int statusCode,
                            int status, String description) throws Exception {
        // 测试数据
//        String name = "tom0001";
//        String phone = "13020210001";
//        String pwd = "test123";
//        String rePwd = "test123";
//        int role = 1;

        // 发送请求
        ResponseData responseData = userApi.addUser(name, phone, pwd, rePwd, role);

        // 断言
        Assert.assertEquals(responseData.getCode(), statusCode);
        Assert.assertEquals(responseData.getBody().getIntValue("status"), status);
        Assert.assertEquals(responseData.getBody().getString("description"), description);
    }

    @Test
    public void testAddUserSuccess() throws Exception {
        // 测试数据
        String name = "tom0001";
        String phone = "13020210001";
        String pwd = "test123";
        String rePwd = "test123";
        int role = 1;

        // 发送请求
        ResponseData responseData = userApi.addUser(name, phone, pwd, rePwd, role);

        // 断言
        Assert.assertEquals(responseData.getCode(), 200);
        Assert.assertEquals(responseData.getBody().getIntValue("status"), 200);
        Assert.assertEquals(responseData.getBody().getString("description"), "添加成功");
    }

}
